package com.packet.cheng.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import com.alibaba.fastjson.JSON;
import com.packet.cheng.dto.UserParamDTO;
import com.packet.cheng.entity.TestUser;
import com.packet.cheng.entity.User;
import com.packet.cheng.service.UserService;
import com.packet.cheng.util.MD5Util;
import com.packet.cheng.util.R;
import com.packet.cheng.util.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author 张在成
 */
@RestController
@RequestMapping("/user")
//@CrossOrigin(origins = "*")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    public  R<?>  loginUser(@RequestHeader("token") String token,@RequestParam("username") String username,@RequestParam("password") String password) throws Exception {
        if (StringUtil.isBlank(username)||StringUtil.isBlank(password)){
            return R.data(406,"用户名和密码不能为空");
        }
        return userService.login(token,username,password);
    }

    @PostMapping("/add")
    public R<?>  addUser(@RequestBody UserParamDTO paramDTO){
        try {
            if (paramDTO.getUsername()==null||paramDTO.getPassword()==null||paramDTO.getNickName()==null||paramDTO.getAvtarUrl()==null||paramDTO.getPhone()==null){
                throw  new Exception("您的参数有误");
            }
            boolean b = userService.insertUser(paramDTO);
            if (b){
                return  R.data(200,"新增用户信息成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

    @GetMapping(value = "selectUser")
    public  R<?>  selectUser(){
//        System.out.println(request.getLocalAddr()+"--"+request.getLocalPort());
        List<User> list = userService.selectUser();
        return  R.data(200,"获取用户信息成功",list);
    }

    @PostMapping(value = "selectUserById")
    public  R<?>  selectUserById(@RequestBody UserParamDTO paramDTO) throws Exception {
        User user = userService.selectUserById(paramDTO);
        return  R.data(200,"获取用户信息成功",user);
    }

    @GetMapping(value = "selectUserByName")
    public  R<?>  selectUserByName(@RequestParam("username") String username) throws Exception {
        List<User> user = userService.selectUserByName(username);
        return  R.data(200,"获取用户信息成功",user);
    }

    @PostMapping(value = "updateUser")
    public  R<?>  updateUser(@RequestBody User paramDTO) throws Exception {
        userService.updateUser(paramDTO);
        return  R.data(200,"更新User成功",1);
    }

    @PostMapping(value = "updateTestUser")
    public  R<?>  updateTestUser(@RequestBody TestUser testUser) throws Exception {
        userService.updateTestUser(testUser);
        return  R.data(200,"更新TestUser信息成功",1);
    }

    @GetMapping("validateCode")
    public  void  getValidateCode(HttpServletRequest request, HttpServletResponse response){
        // 线段干扰式验证码
        //LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200,100);
        // 圆圈干扰验证码
        //CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200,100,4,20);
        // 扭曲干扰验证码
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200,100,4,5);
        try {
            request.getSession().setAttribute("CAPTCHA_KEY",captcha.getCode());
            response.setContentType("image/png");
            response.setHeader("Pragma","No-cache");
            response.setHeader("Cache-Control","no-cache");
            response.setDateHeader("Expire",0);
            captcha.write(response.getOutputStream());

            System.out.println("获取到验证码："+captcha.getCode());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
